{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Please input your directory for the top level folder\n",
    "folder name : SUBMISSION MODEL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_ = 'INPUT-PROJECT-DIRECTORY/submission_model/' # input only here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### setting other directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_dir = dir_+'2. data/'\n",
    "processed_data_dir = dir_+'2. data/processed/'\n",
    "log_dir = dir_+'4. logs/'\n",
    "model_dir = dir_+'5. models/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "####################################################################################\n",
    "#################### 2-2. nonrecursive model by store & cat ########################\n",
    "####################################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cvs = ['private']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "STORES = ['CA_1', 'CA_2', 'CA_3', 'CA_4', 'TX_1', 'TX_2', 'TX_3', 'WI_1', 'WI_2', 'WI_3']\n",
    "CATS = ['HOBBIES', 'HOUSEHOLD', 'FOODS']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from  datetime import datetime, timedelta\n",
    "import gc\n",
    "import numpy as np, pandas as pd\n",
    "import lightgbm as lgb\n",
    "\n",
    "import os, sys, gc, time, warnings, pickle, psutil, random\n",
    "\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reduce_mem_usage(df, verbose=False):\n",
    "    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n",
    "    start_mem = df.memory_usage().sum() / 1024**2    \n",
    "    for col in df.columns:\n",
    "        col_type = df[col].dtypes\n",
    "        if col_type in numerics:\n",
    "            c_min = df[col].min()\n",
    "            c_max = df[col].max()\n",
    "            if str(col_type)[:3] == 'int':\n",
    "                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n",
    "                    df[col] = df[col].astype(np.int8)\n",
    "                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n",
    "                       df[col] = df[col].astype(np.int16)\n",
    "                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n",
    "                    df[col] = df[col].astype(np.int32)\n",
    "                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n",
    "                    df[col] = df[col].astype(np.int64)  \n",
    "            else:\n",
    "                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n",
    "                    df[col] = df[col].astype(np.float16)\n",
    "                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n",
    "                    df[col] = df[col].astype(np.float32)\n",
    "                else:\n",
    "                    df[col] = df[col].astype(np.float64)    \n",
    "    end_mem = df.memory_usage().sum() / 1024**2\n",
    "    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
   },
   "outputs": [],
   "source": [
    "FIRST_DAY = 710 \n",
    "remove_feature = ['id',\n",
    "                  'state_id',\n",
    "                  'store_id',\n",
    "#                   'item_id',\n",
    "#                   'dept_id',\n",
    "                  'cat_id',\n",
    "                  'date','wm_yr_wk','d','sales']\n",
    "\n",
    "cat_var = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id'] + [\"event_name_1\", \"event_name_2\", \"event_type_1\", \"event_type_2\"]\n",
    "cat_var = list(set(cat_var) - set(remove_feature))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid2_colnm = ['sell_price', 'price_max', 'price_min', 'price_std',\n",
    "               'price_mean', 'price_norm', 'price_nunique', 'item_nunique',\n",
    "               'price_momentum', 'price_momentum_m', 'price_momentum_y']\n",
    "\n",
    "grid3_colnm = ['event_name_1', 'event_type_1', 'event_name_2',\n",
    "               'event_type_2', 'snap_CA', 'snap_TX', 'snap_WI', 'tm_d', 'tm_w', 'tm_m',\n",
    "               'tm_y', 'tm_wm', 'tm_dw', 'tm_w_end']\n",
    "\n",
    "lag_colnm = [ 'sales_lag_28', 'sales_lag_29', 'sales_lag_30',\n",
    "             'sales_lag_31', 'sales_lag_32', 'sales_lag_33', 'sales_lag_34',\n",
    "             'sales_lag_35', 'sales_lag_36', 'sales_lag_37', 'sales_lag_38',\n",
    "             'sales_lag_39', 'sales_lag_40', 'sales_lag_41', 'sales_lag_42',\n",
    "             \n",
    "             'rolling_mean_7', 'rolling_std_7', 'rolling_mean_14', 'rolling_std_14',\n",
    "             'rolling_mean_30', 'rolling_std_30', 'rolling_mean_60',\n",
    "             'rolling_std_60', 'rolling_mean_180', 'rolling_std_180']\n",
    "\n",
    "mean_enc_colnm = [\n",
    "    \n",
    "    'enc_store_id_dept_id_mean', 'enc_store_id_dept_id_std', \n",
    "    'enc_item_id_store_id_mean', 'enc_item_id_store_id_std'\n",
    "\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "########################### Make grid\n",
    "#################################################################################\n",
    "def prepare_data(store, state):\n",
    "    \n",
    "    grid_1 = pd.read_pickle(processed_data_dir+\"grid_part_1.pkl\")\n",
    "    grid_2 = pd.read_pickle(processed_data_dir+\"grid_part_2.pkl\")[grid2_colnm]\n",
    "    grid_3 = pd.read_pickle(processed_data_dir+\"grid_part_3.pkl\")[grid3_colnm]\n",
    "\n",
    "    grid_df = pd.concat([grid_1, grid_2, grid_3], axis=1)\n",
    "    del grid_1, grid_2, grid_3; gc.collect()\n",
    "    \n",
    "    grid_df = grid_df[(grid_df['store_id'] == store) & (grid_df['cat_id'] == state)]\n",
    "    grid_df = grid_df[grid_df['d'] >= FIRST_DAY]\n",
    "    \n",
    "    lag = pd.read_pickle(processed_data_dir+\"lags_df_28.pkl\")[lag_colnm]\n",
    "    \n",
    "    lag = lag[lag.index.isin(grid_df.index)]\n",
    "    \n",
    "    grid_df = pd.concat([grid_df,\n",
    "                     lag],\n",
    "                    axis=1)\n",
    "    \n",
    "    del lag; gc.collect()\n",
    "    \n",
    "\n",
    "    mean_enc = pd.read_pickle(processed_data_dir+\"mean_encoding_df.pkl\")[mean_enc_colnm]\n",
    "    mean_enc = mean_enc[mean_enc.index.isin(grid_df.index)]\n",
    "    \n",
    "    grid_df = pd.concat([grid_df,\n",
    "                         mean_enc],\n",
    "                        axis=1)    \n",
    "    del mean_enc; gc.collect()\n",
    "    \n",
    "    grid_df = reduce_mem_usage(grid_df)\n",
    "    \n",
    "    \n",
    "    \n",
    "    return grid_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "validation = {\n",
    "    'cv1' : [1551, 1610],\n",
    "    'cv2' : [1829,1857],\n",
    "    'cv3' : [1857, 1885],\n",
    "    'cv4' : [1885,1913],\n",
    "    'public' : [1913, 1941],\n",
    "    'private' : [1941, 1969]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cv1 : 2015-04-28 ~ 2015-06-26\n",
    "\n",
    "### cv2 : 2016-02-01 ~ 2016-02-28\n",
    "\n",
    "### cv3 : 2016-02-29 ~ 2016-03-27\n",
    "\n",
    "### cv4 : 2016-03-28 ~ 2016-04-24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "########################### Model params\n",
    "#################################################################################\n",
    "lgb_params = {\n",
    "                    'boosting_type': 'gbdt',\n",
    "                    'objective': 'tweedie',\n",
    "                    'tweedie_variance_power': 1.1,\n",
    "                    'metric': 'rmse',\n",
    "                    'subsample': 0.5,\n",
    "                    'subsample_freq': 1,\n",
    "                    'learning_rate': 0.015,\n",
    "                    'num_leaves': 2**8-1,\n",
    "                    'min_data_in_leaf': 2**8-1,\n",
    "                    'feature_fraction': 0.5,\n",
    "                    'max_bin': 100,\n",
    "                    'n_estimators': 3000,\n",
    "                    'boost_from_average': False,\n",
    "                    'verbose': -1,\n",
    "                    'seed' : 1995\n",
    "                } "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cv : day [1941, 1969]\n",
      "CA_1 HOBBIES start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.5369\tvalid_1's rmse: 2.24761\n",
      "[200]\tvalid_0's rmse: 1.81648\tvalid_1's rmse: 2.15747\n",
      "[300]\tvalid_0's rmse: 1.88827\tvalid_1's rmse: 2.10966\n",
      "[400]\tvalid_0's rmse: 1.90436\tvalid_1's rmse: 2.06366\n",
      "[500]\tvalid_0's rmse: 1.91182\tvalid_1's rmse: 2.0216\n",
      "[600]\tvalid_0's rmse: 1.916\tvalid_1's rmse: 1.98112\n",
      "[700]\tvalid_0's rmse: 1.91412\tvalid_1's rmse: 1.94194\n",
      "[800]\tvalid_0's rmse: 1.91573\tvalid_1's rmse: 1.90427\n",
      "[900]\tvalid_0's rmse: 1.91487\tvalid_1's rmse: 1.86728\n",
      "[1000]\tvalid_0's rmse: 1.91168\tvalid_1's rmse: 1.8307\n",
      "[1100]\tvalid_0's rmse: 1.91365\tvalid_1's rmse: 1.79582\n",
      "[1200]\tvalid_0's rmse: 1.91102\tvalid_1's rmse: 1.76149\n",
      "[1300]\tvalid_0's rmse: 1.91078\tvalid_1's rmse: 1.72846\n",
      "[1400]\tvalid_0's rmse: 1.90267\tvalid_1's rmse: 1.69676\n",
      "[1500]\tvalid_0's rmse: 1.90127\tvalid_1's rmse: 1.66545\n",
      "[1600]\tvalid_0's rmse: 1.89771\tvalid_1's rmse: 1.63566\n",
      "[1700]\tvalid_0's rmse: 1.89598\tvalid_1's rmse: 1.60626\n",
      "[1800]\tvalid_0's rmse: 1.88783\tvalid_1's rmse: 1.578\n",
      "[1900]\tvalid_0's rmse: 1.887\tvalid_1's rmse: 1.55112\n",
      "[2000]\tvalid_0's rmse: 1.88056\tvalid_1's rmse: 1.52517\n",
      "[2100]\tvalid_0's rmse: 1.87512\tvalid_1's rmse: 1.49994\n",
      "[2200]\tvalid_0's rmse: 1.87205\tvalid_1's rmse: 1.47658\n",
      "[2300]\tvalid_0's rmse: 1.86974\tvalid_1's rmse: 1.45345\n",
      "[2400]\tvalid_0's rmse: 1.86433\tvalid_1's rmse: 1.43174\n",
      "[2500]\tvalid_0's rmse: 1.86093\tvalid_1's rmse: 1.41112\n",
      "[2600]\tvalid_0's rmse: 1.85768\tvalid_1's rmse: 1.39065\n",
      "[2700]\tvalid_0's rmse: 1.8536\tvalid_1's rmse: 1.37184\n",
      "[2800]\tvalid_0's rmse: 1.84911\tvalid_1's rmse: 1.3538\n",
      "[2900]\tvalid_0's rmse: 1.84716\tvalid_1's rmse: 1.33635\n",
      "[3000]\tvalid_0's rmse: 1.84173\tvalid_1's rmse: 1.32016\n",
      "CA_1 HOUSEHOLD start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.39688\tvalid_1's rmse: 1.32864\n",
      "[200]\tvalid_0's rmse: 1.6383\tvalid_1's rmse: 1.26545\n",
      "[300]\tvalid_0's rmse: 1.70321\tvalid_1's rmse: 1.24652\n",
      "[400]\tvalid_0's rmse: 1.71859\tvalid_1's rmse: 1.23332\n",
      "[500]\tvalid_0's rmse: 1.72571\tvalid_1's rmse: 1.22124\n",
      "[600]\tvalid_0's rmse: 1.72398\tvalid_1's rmse: 1.20998\n",
      "[700]\tvalid_0's rmse: 1.72613\tvalid_1's rmse: 1.19892\n",
      "[800]\tvalid_0's rmse: 1.72133\tvalid_1's rmse: 1.18857\n",
      "[900]\tvalid_0's rmse: 1.72326\tvalid_1's rmse: 1.17979\n",
      "[1000]\tvalid_0's rmse: 1.71972\tvalid_1's rmse: 1.17019\n",
      "[1100]\tvalid_0's rmse: 1.71674\tvalid_1's rmse: 1.16219\n",
      "[1200]\tvalid_0's rmse: 1.71778\tvalid_1's rmse: 1.15551\n",
      "[1300]\tvalid_0's rmse: 1.71285\tvalid_1's rmse: 1.14929\n",
      "[1400]\tvalid_0's rmse: 1.71411\tvalid_1's rmse: 1.14388\n",
      "[1500]\tvalid_0's rmse: 1.7132\tvalid_1's rmse: 1.1379\n",
      "[1600]\tvalid_0's rmse: 1.71366\tvalid_1's rmse: 1.13277\n",
      "[1700]\tvalid_0's rmse: 1.71081\tvalid_1's rmse: 1.1275\n",
      "[1800]\tvalid_0's rmse: 1.71043\tvalid_1's rmse: 1.12256\n",
      "[1900]\tvalid_0's rmse: 1.71052\tvalid_1's rmse: 1.11781\n",
      "[2000]\tvalid_0's rmse: 1.70858\tvalid_1's rmse: 1.11332\n",
      "[2100]\tvalid_0's rmse: 1.70634\tvalid_1's rmse: 1.10919\n",
      "[2200]\tvalid_0's rmse: 1.70574\tvalid_1's rmse: 1.10489\n",
      "[2300]\tvalid_0's rmse: 1.70391\tvalid_1's rmse: 1.10104\n",
      "[2400]\tvalid_0's rmse: 1.70522\tvalid_1's rmse: 1.09725\n",
      "[2500]\tvalid_0's rmse: 1.70506\tvalid_1's rmse: 1.09349\n",
      "[2600]\tvalid_0's rmse: 1.70351\tvalid_1's rmse: 1.0897\n",
      "[2700]\tvalid_0's rmse: 1.70177\tvalid_1's rmse: 1.08618\n",
      "[2800]\tvalid_0's rmse: 1.70051\tvalid_1's rmse: 1.08257\n",
      "[2900]\tvalid_0's rmse: 1.70033\tvalid_1's rmse: 1.07948\n",
      "[3000]\tvalid_0's rmse: 1.70203\tvalid_1's rmse: 1.07596\n",
      "CA_1 FOODS start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 3.23997\tvalid_1's rmse: 3.51876\n",
      "[200]\tvalid_0's rmse: 4.11164\tvalid_1's rmse: 3.11023\n",
      "[300]\tvalid_0's rmse: 4.32362\tvalid_1's rmse: 2.97352\n",
      "[400]\tvalid_0's rmse: 4.38181\tvalid_1's rmse: 2.89496\n",
      "[500]\tvalid_0's rmse: 4.39777\tvalid_1's rmse: 2.83882\n",
      "[600]\tvalid_0's rmse: 4.4076\tvalid_1's rmse: 2.79801\n",
      "[700]\tvalid_0's rmse: 4.40499\tvalid_1's rmse: 2.76239\n",
      "[800]\tvalid_0's rmse: 4.40109\tvalid_1's rmse: 2.7335\n",
      "[900]\tvalid_0's rmse: 4.38993\tvalid_1's rmse: 2.7062\n",
      "[1000]\tvalid_0's rmse: 4.38322\tvalid_1's rmse: 2.6852\n",
      "[1100]\tvalid_0's rmse: 4.36983\tvalid_1's rmse: 2.6651\n",
      "[1200]\tvalid_0's rmse: 4.36274\tvalid_1's rmse: 2.64612\n",
      "[1300]\tvalid_0's rmse: 4.3601\tvalid_1's rmse: 2.62992\n",
      "[1400]\tvalid_0's rmse: 4.34532\tvalid_1's rmse: 2.6155\n",
      "[1500]\tvalid_0's rmse: 4.33931\tvalid_1's rmse: 2.60078\n",
      "[1600]\tvalid_0's rmse: 4.32786\tvalid_1's rmse: 2.58649\n",
      "[1700]\tvalid_0's rmse: 4.32513\tvalid_1's rmse: 2.57451\n",
      "[1800]\tvalid_0's rmse: 4.32161\tvalid_1's rmse: 2.56324\n",
      "[1900]\tvalid_0's rmse: 4.31877\tvalid_1's rmse: 2.55139\n",
      "[2000]\tvalid_0's rmse: 4.31623\tvalid_1's rmse: 2.54189\n",
      "[2100]\tvalid_0's rmse: 4.30722\tvalid_1's rmse: 2.53293\n",
      "[2200]\tvalid_0's rmse: 4.30021\tvalid_1's rmse: 2.52346\n",
      "[2300]\tvalid_0's rmse: 4.3001\tvalid_1's rmse: 2.51392\n",
      "[2400]\tvalid_0's rmse: 4.2962\tvalid_1's rmse: 2.50561\n",
      "[2500]\tvalid_0's rmse: 4.29118\tvalid_1's rmse: 2.49713\n",
      "[2600]\tvalid_0's rmse: 4.28602\tvalid_1's rmse: 2.4893\n",
      "[2700]\tvalid_0's rmse: 4.27622\tvalid_1's rmse: 2.48154\n",
      "[2800]\tvalid_0's rmse: 4.27422\tvalid_1's rmse: 2.47432\n",
      "[2900]\tvalid_0's rmse: 4.27007\tvalid_1's rmse: 2.46748\n",
      "[3000]\tvalid_0's rmse: 4.26769\tvalid_1's rmse: 2.46087\n",
      "CA_2 HOBBIES start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.13721\tvalid_1's rmse: 1.74979\n",
      "[200]\tvalid_0's rmse: 1.29487\tvalid_1's rmse: 1.69287\n",
      "[300]\tvalid_0's rmse: 1.34451\tvalid_1's rmse: 1.65761\n",
      "[400]\tvalid_0's rmse: 1.35718\tvalid_1's rmse: 1.62226\n",
      "[500]\tvalid_0's rmse: 1.36907\tvalid_1's rmse: 1.58694\n",
      "[600]\tvalid_0's rmse: 1.36993\tvalid_1's rmse: 1.55445\n",
      "[700]\tvalid_0's rmse: 1.36782\tvalid_1's rmse: 1.5224\n",
      "[800]\tvalid_0's rmse: 1.3679\tvalid_1's rmse: 1.49274\n",
      "[900]\tvalid_0's rmse: 1.36369\tvalid_1's rmse: 1.46299\n",
      "[1000]\tvalid_0's rmse: 1.35619\tvalid_1's rmse: 1.43423\n",
      "[1100]\tvalid_0's rmse: 1.35609\tvalid_1's rmse: 1.40637\n",
      "[1200]\tvalid_0's rmse: 1.34927\tvalid_1's rmse: 1.37975\n",
      "[1300]\tvalid_0's rmse: 1.34616\tvalid_1's rmse: 1.35379\n",
      "[1400]\tvalid_0's rmse: 1.34048\tvalid_1's rmse: 1.32937\n",
      "[1500]\tvalid_0's rmse: 1.33555\tvalid_1's rmse: 1.30529\n",
      "[1600]\tvalid_0's rmse: 1.33259\tvalid_1's rmse: 1.28274\n",
      "[1700]\tvalid_0's rmse: 1.32747\tvalid_1's rmse: 1.26072\n",
      "[1800]\tvalid_0's rmse: 1.3223\tvalid_1's rmse: 1.24001\n",
      "[1900]\tvalid_0's rmse: 1.32066\tvalid_1's rmse: 1.21989\n",
      "[2000]\tvalid_0's rmse: 1.31529\tvalid_1's rmse: 1.20069\n",
      "[2100]\tvalid_0's rmse: 1.3102\tvalid_1's rmse: 1.1825\n",
      "[2200]\tvalid_0's rmse: 1.30684\tvalid_1's rmse: 1.16498\n",
      "[2300]\tvalid_0's rmse: 1.30339\tvalid_1's rmse: 1.14775\n",
      "[2400]\tvalid_0's rmse: 1.30217\tvalid_1's rmse: 1.13172\n",
      "[2500]\tvalid_0's rmse: 1.29593\tvalid_1's rmse: 1.11647\n",
      "[2600]\tvalid_0's rmse: 1.29426\tvalid_1's rmse: 1.10124\n",
      "[2700]\tvalid_0's rmse: 1.29154\tvalid_1's rmse: 1.08755\n",
      "[2800]\tvalid_0's rmse: 1.28764\tvalid_1's rmse: 1.07431\n",
      "[2900]\tvalid_0's rmse: 1.2859\tvalid_1's rmse: 1.06191\n",
      "[3000]\tvalid_0's rmse: 1.28325\tvalid_1's rmse: 1.04995\n",
      "CA_2 HOUSEHOLD start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.3698\tvalid_1's rmse: 1.42348\n",
      "[200]\tvalid_0's rmse: 1.57745\tvalid_1's rmse: 1.35479\n",
      "[300]\tvalid_0's rmse: 1.66017\tvalid_1's rmse: 1.32589\n",
      "[400]\tvalid_0's rmse: 1.69123\tvalid_1's rmse: 1.3051\n",
      "[500]\tvalid_0's rmse: 1.70695\tvalid_1's rmse: 1.28833\n",
      "[600]\tvalid_0's rmse: 1.71389\tvalid_1's rmse: 1.27519\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[700]\tvalid_0's rmse: 1.71832\tvalid_1's rmse: 1.2639\n",
      "[800]\tvalid_0's rmse: 1.71799\tvalid_1's rmse: 1.25326\n",
      "[900]\tvalid_0's rmse: 1.71619\tvalid_1's rmse: 1.24381\n",
      "[1000]\tvalid_0's rmse: 1.71819\tvalid_1's rmse: 1.23511\n",
      "[1100]\tvalid_0's rmse: 1.71667\tvalid_1's rmse: 1.22709\n",
      "[1200]\tvalid_0's rmse: 1.71718\tvalid_1's rmse: 1.21942\n",
      "[1300]\tvalid_0's rmse: 1.71844\tvalid_1's rmse: 1.2126\n",
      "[1400]\tvalid_0's rmse: 1.71855\tvalid_1's rmse: 1.20612\n",
      "[1500]\tvalid_0's rmse: 1.71834\tvalid_1's rmse: 1.1997\n",
      "[1600]\tvalid_0's rmse: 1.71644\tvalid_1's rmse: 1.19364\n",
      "[1700]\tvalid_0's rmse: 1.71665\tvalid_1's rmse: 1.18793\n",
      "[1800]\tvalid_0's rmse: 1.71625\tvalid_1's rmse: 1.18253\n",
      "[1900]\tvalid_0's rmse: 1.71499\tvalid_1's rmse: 1.17733\n",
      "[2000]\tvalid_0's rmse: 1.71619\tvalid_1's rmse: 1.17207\n",
      "[2100]\tvalid_0's rmse: 1.71498\tvalid_1's rmse: 1.16701\n",
      "[2200]\tvalid_0's rmse: 1.71657\tvalid_1's rmse: 1.16227\n",
      "[2300]\tvalid_0's rmse: 1.71488\tvalid_1's rmse: 1.15794\n",
      "[2400]\tvalid_0's rmse: 1.71469\tvalid_1's rmse: 1.15358\n",
      "[2500]\tvalid_0's rmse: 1.71278\tvalid_1's rmse: 1.14924\n",
      "[2600]\tvalid_0's rmse: 1.71242\tvalid_1's rmse: 1.1448\n",
      "[2700]\tvalid_0's rmse: 1.71092\tvalid_1's rmse: 1.14078\n",
      "[2800]\tvalid_0's rmse: 1.71116\tvalid_1's rmse: 1.13688\n",
      "[2900]\tvalid_0's rmse: 1.71005\tvalid_1's rmse: 1.13289\n",
      "[3000]\tvalid_0's rmse: 1.70934\tvalid_1's rmse: 1.12905\n",
      "[1200]\tvalid_0's rmse: 4.01681\tvalid_1's rmse: 2.10655\n",
      "[1300]\tvalid_0's rmse: 4.02115\tvalid_1's rmse: 2.0919\n",
      "[1400]\tvalid_0's rmse: 4.01741\tvalid_1's rmse: 2.07858\n",
      "[1500]\tvalid_0's rmse: 4.00329\tvalid_1's rmse: 2.0661\n",
      "[1600]\tvalid_0's rmse: 4.00297\tvalid_1's rmse: 2.05407\n",
      "[1700]\tvalid_0's rmse: 4.00147\tvalid_1's rmse: 2.04338\n",
      "[1800]\tvalid_0's rmse: 4.00141\tvalid_1's rmse: 2.03334\n",
      "[1900]\tvalid_0's rmse: 3.99778\tvalid_1's rmse: 2.02406\n",
      "[2000]\tvalid_0's rmse: 3.99537\tvalid_1's rmse: 2.01443\n",
      "[2100]\tvalid_0's rmse: 3.99917\tvalid_1's rmse: 2.0055\n",
      "[2200]\tvalid_0's rmse: 3.99279\tvalid_1's rmse: 1.9962\n",
      "[2300]\tvalid_0's rmse: 3.99084\tvalid_1's rmse: 1.98751\n",
      "[2400]\tvalid_0's rmse: 3.98821\tvalid_1's rmse: 1.97964\n",
      "[2500]\tvalid_0's rmse: 3.9911\tvalid_1's rmse: 1.97193\n",
      "[2600]\tvalid_0's rmse: 3.98941\tvalid_1's rmse: 1.96413\n",
      "[2700]\tvalid_0's rmse: 3.98819\tvalid_1's rmse: 1.95683\n",
      "CA_3 HOBBIES start\n",
      "[LightGBM] [Warning] feature_fraction is set=0.5, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.5\n",
      "[LightGBM] [Warning] min_data_in_leaf is set=255, min_child_samples=20 will be ignored. Current value: min_data_in_leaf=255\n",
      "[100]\tvalid_0's rmse: 1.62073\tvalid_1's rmse: 2.52492\n",
      "[200]\tvalid_0's rmse: 1.90789\tvalid_1's rmse: 2.40758\n",
      "[300]\tvalid_0's rmse: 1.97713\tvalid_1's rmse: 2.34226\n",
      "[400]\tvalid_0's rmse: 1.99298\tvalid_1's rmse: 2.27692\n",
      "[500]\tvalid_0's rmse: 2.00234\tvalid_1's rmse: 2.21983\n",
      "[600]\tvalid_0's rmse: 2.00676\tvalid_1's rmse: 2.16548\n",
      "[700]\tvalid_0's rmse: 2.01106\tvalid_1's rmse: 2.11029\n",
      "[800]\tvalid_0's rmse: 2.00539\tvalid_1's rmse: 2.05905\n",
      "[900]\tvalid_0's rmse: 1.99975\tvalid_1's rmse: 2.01008\n",
      "[1000]\tvalid_0's rmse: 1.99764\tvalid_1's rmse: 1.96068\n",
      "[1100]\tvalid_0's rmse: 1.99859\tvalid_1's rmse: 1.91588\n",
      "[1200]\tvalid_0's rmse: 1.99262\tvalid_1's rmse: 1.87248\n",
      "[1300]\tvalid_0's rmse: 1.98901\tvalid_1's rmse: 1.82947\n",
      "[1400]\tvalid_0's rmse: 1.97882\tvalid_1's rmse: 1.78984\n",
      "[1500]\tvalid_0's rmse: 1.97367\tvalid_1's rmse: 1.75481\n",
      "[1600]\tvalid_0's rmse: 1.96697\tvalid_1's rmse: 1.71681\n"
     ]
    }
   ],
   "source": [
    "########################### Train Models\n",
    "#################################################################################\n",
    "from lightgbm import LGBMRegressor\n",
    "from gluonts.model.rotbaum._model import QRX\n",
    "rmsse_bycv = dict()\n",
    "\n",
    "for cv in cvs:\n",
    "    print('cv : day', validation[cv])\n",
    "    \n",
    "    pred_list = []\n",
    "    for store in STORES:\n",
    "        for state in CATS:\n",
    "\n",
    "            print(store,state, 'start')\n",
    "            grid_df = prepare_data(store, state)\n",
    "\n",
    "            model_var = grid_df.columns[~grid_df.columns.isin(remove_feature)]\n",
    "\n",
    "            tr_mask = (grid_df['d'] <= validation[cv][0]) & (grid_df['d'] >= FIRST_DAY)\n",
    "            vl_mask = (grid_df['d'] > validation[cv][0]) & (grid_df['d'] <= validation[cv][1])\n",
    "\n",
    "            estimator = QRX(model=LGBMRegressor(**lgb_params),\n",
    "                        min_bin_size=200)\n",
    "            estimator.fit(\n",
    "                grid_df[tr_mask][model_var], \n",
    "                grid_df[tr_mask]['sales'],\n",
    "                max_sample_size=1000000, \n",
    "                seed=1,\n",
    "                eval_set=[(\n",
    "                        grid_df[vl_mask][model_var],\n",
    "                        grid_df[vl_mask]['sales']\n",
    "                    ),\n",
    "                    (\n",
    "                        grid_df[tr_mask][model_var], \n",
    "                        grid_df[tr_mask]['sales']\n",
    "                    )\n",
    "                ],\n",
    "                verbose=100,\n",
    "                x_train_is_dataframe=True\n",
    "            )\n",
    "\n",
    "            model_name = model_dir+'non_recur_model_'+store+'_'+state+'.bin'\n",
    "            pickle.dump(estimator, open(model_name, 'wb'))\n",
    "            \n",
    "#            display(pd.DataFrame({'name':m_lgb.feature_name(),\n",
    "#                                  'imp':m_lgb.feature_importance()}).sort_values('imp',ascending=False).head(25))\n",
    "            \n",
    "            del grid_df, estimator, tr_mask, vl_mask; gc.collect() #train_data, valid_data,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "conda_mxnet_p36",
   "language": "python",
   "name": "conda_mxnet_p36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
